
import java.awt.*;
import javax.swing.*;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.lang.Math;
import java.util.Vector;

public class GraphDisplay extends JPanel {

	private static final long serialVersionUID = 1L;
	private Vector <Double> xCoord, yCoord;
	private Line2D.Double xAxis = new Line2D.Double(220, 25, 220, 275);
	private Line2D.Double yAxis = new Line2D.Double(95, 150, 345, 150);
	
	public GraphDisplay() {
		xCoord = new Vector <Double> ();
		yCoord = new Vector <Double> ();
	}
	
	//paint the initial graphics image
	public void paintComponent(Graphics g) {
		super.paintComponents(g);
		Graphics2D g2d = (Graphics2D) g;
		//draw circle
		g2d.setColor(Color.LIGHT_GRAY);
		for (double i = 0; i <= 2*Math.PI; i+=0.05) {
			Line2D.Double lineSeg = new Line2D.Double(125*Math.cos(i-0.05)+220, 
					125*Math.sin(i-0.05)+150, 125*Math.cos(i)+220, 125*Math.sin(i)+150);
			g2d.draw(lineSeg);
		}
		//draw axes
		g2d.draw(xAxis);
		g2d.draw(yAxis);
	}
	
	//Take in the inputs and graph
	public void plot(double start, double end, double coeff) {	
		if(end < start) {
			double temp = start;
			start = end;
		}
		
		double radius, x, y;
		xCoord.removeAllElements();
		yCoord.removeAllElements();
		//calculate the points
		for (double i = start; i <= end; i+=0.05){
			radius = Math.cos(coeff*i);
			x = 125*(radius*Math.cos(i) )+ 220;
			y = 125*(radius*Math.sin(i) )+ 150;
			xCoord.add(x);
			yCoord.add(y);
		}
		
		Graphics g = getGraphics();
		Graphics2D g2d = (Graphics2D) g;
		g2d.setPaint(Color.WHITE);
		g2d.fill(new Rectangle2D.Double(20, 2, 400, 310));
		
		g2d.setColor(Color.LIGHT_GRAY);
		g2d.setStroke(new BasicStroke(1));
		//draws the circle
		for (double i = 0; i <= 2*Math.PI; i+=0.05) {
			Line2D.Double circle = new Line2D.Double(125*Math.cos(i-0.05)+220, 
					125*Math.sin(i-0.05)+150, 125*Math.cos(i)+220, 125*Math.sin(i)+150);
			g2d.draw(circle);
		}
		//draw axes
		g2d.draw(xAxis);
		g2d.draw(yAxis);
		//draw graph
		g2d.setColor(new Color (22,147,165));
		g2d.setStroke(new BasicStroke(2));
		for (int i = 1; i < xCoord.size(); ++i) {
			Line2D.Double lineSeg = new Line2D.Double(xCoord.elementAt(i-1), yCoord.elementAt(i-1),
					xCoord.elementAt(i), yCoord.elementAt(i));
			g2d.draw(lineSeg);
		}
	}
}
